package com.ouyeel.wlxer.activity;

import android.support.v7.widget.RecyclerView;
import android.text.InputType;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.ouyeel.wlxer.R;
import com.ouyeel.wlxer.adapter.MyLineAdapter;
import com.ouyeel.wlxer.base.BaseActivity;
import com.ouyeel.wlxer.bean.MyLine;
import com.ouyeel.wlxer.util.ChangeAddressPopwindow;
import com.ouyeel.wlxer.util.StringUtils;
import com.ouyeel.wlxer.widget.MyLinearLayoutManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.BindViews;

/**
 * Created by rankaifeng on 2017/11/17.
 */

public class PersonalInfoActivity extends BaseActivity implements View.OnClickListener {
    private static final String TAG = PersonalInfoActivity.class.getSimpleName();
    private static final int EDIT_POSITION_START = 0;//起始地的文本框
    private static final int EDIT_POSITION_END = 1;//目的地的文本框
    @BindView(R.id.recy_pserson)
    RecyclerView recyPserson;
    MyLineAdapter adapter;
    List<MyLine> myLineList;
    @BindViews({R.id.edit_start, R.id.edit_end})
    List<EditText> editTexts;
    MyLine myLine;
    @BindView(R.id.lin_change_line)
    LinearLayout linChangeLine;

    @Override
    public int getLayout() {
        return R.layout.activity_psersoninfo;
    }

    @Override
    public void initView() {
        showRightText(this.getString(R.string.app_line_edit));
        for (int i = 0; i < editTexts.size(); i++) {
            editTexts.get(i).setInputType(InputType.TYPE_NULL);
            editTexts.get(i).setFocusable(false);
            editTexts.get(i).setOnClickListener(this);
        }
        recyPserson.setLayoutManager(new MyLinearLayoutManager(this));
        myLineList = new ArrayList<>();
        adapter = new MyLineAdapter(this, R.layout.myline, myLineList);
        recyPserson.setAdapter(adapter);
    }

    private void showPopu(final EditText editText) {
        ChangeAddressPopwindow mChangeAddressPopwindow = new ChangeAddressPopwindow(this);
        mChangeAddressPopwindow.setAddress("广东", "深圳", "福田区");
        mChangeAddressPopwindow.showAtLocation(editText, Gravity.BOTTOM, 0, 0);
        mChangeAddressPopwindow
                .setAddresskListener(new ChangeAddressPopwindow.OnAddressCListener() {
                    @Override
                    public void onClick(String province, String city, String area) {
                        // TODO Auto-generated method stub
                        editText.setText(province + city + area);
                        String editStartStr = editTexts.get(EDIT_POSITION_START).getText().toString().trim();
                        String editEndStr = editTexts.get(EDIT_POSITION_END).getText().toString().trim();
                        if (StringUtils.isNotEmpty(editStartStr)
                                && StringUtils.isNotEmpty(editEndStr)) {
                            myLine = new MyLine();
                            myLine.setStartPlace(editStartStr);
                            myLine.setEndPlace(editEndStr);
                            myLineList.add(myLine);
                            adapter.notifyDataSetChanged();
                            for (int i = 0; i < editTexts.size(); i++) {
                                editTexts.get(i).setText("");
                            }
                        } else {
                            return;
                        }
                    }
                });
    }

    /**
     * 右边文字点击事件
     */
    @Override
    public void rightTextOnclick() {
        super.rightTextOnclick();
        String rightText = getRightText();
        if (rightText.equals(this.getString(R.string.app_line_edit))) {
            linChangeLine.setVisibility(View.VISIBLE);
            showRightText(this.getString(R.string.app_line_save));
        } else {
            linChangeLine.setVisibility(View.GONE);
            showRightText(this.getString(R.string.app_line_edit));
        }
    }

    private void showRightText(String text) {
        showTitleLeftBackRight("个人信息", text);
    }

    /**
     * 文本编辑框点击事件
     *
     * @param view
     */
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.edit_start:
                showPopu(editTexts.get(EDIT_POSITION_START));
                break;
            case R.id.edit_end:
                showPopu(editTexts.get(EDIT_POSITION_END));
                break;
        }
    }
}
